package com.qen.conditions.query;

import cn.hutool.core.collection.CollectionUtil;
import com.qen.conditions.AbstractChainWrapper;
import com.qen.interfaces.SFunction;
import com.qen.repository.BaseDao;
import com.qen.wrapper.query.LambdaQueryWrapper;
import com.qen.wrapper.query.interfaces.QueryBase;
import org.beetl.sql.clazz.ColDesc;
import org.beetl.sql.clazz.kit.BeetlSQLException;

import java.util.List;
import java.util.function.Predicate;

/**
 * @author licz
 * @data 2025年05月26日 9:03 AM
 */
public class LambdaQueryChainWrapper<T>
        extends AbstractChainWrapper<T, SFunction<T, ?>, LambdaQueryChainWrapper<T>, LambdaQueryWrapper<T>>
        implements ChainQuery<T>, QueryBase<LambdaQueryChainWrapper<T>, T, SFunction<T, ?>> {

    private final BaseDao<T> baseDao;

    public LambdaQueryChainWrapper(BaseDao<T> baseDao) {
        super();
        this.baseDao = baseDao;
        super.wrapperChildren = new LambdaQueryWrapper<>();
    }

    public LambdaQueryChainWrapper(Class<T> entityClass) {
        super();
        this.baseDao = null;
        super.wrapperChildren = new LambdaQueryWrapper<>(entityClass);
    }

    public LambdaQueryChainWrapper(BaseDao<T> baseDao, T entity) {
        super();
        this.baseDao = baseDao;
        super.wrapperChildren = new LambdaQueryWrapper<>(entity);
    }

    public LambdaQueryChainWrapper(BaseDao<T> baseDao, Class<T> entityClass) {
        super();
        this.baseDao = baseDao;
        super.wrapperChildren = new LambdaQueryWrapper<>(entityClass);
    }

    public LambdaQueryChainWrapper(BaseDao<T> baseDao, LambdaQueryWrapper<T> wrapperChildren) {
        super();
        this.baseDao = baseDao;
        super.wrapperChildren = wrapperChildren;
    }

    @Override
    public LambdaQueryChainWrapper<T> select(boolean condition, List<SFunction<T, ?>> columns) {
        return doSelect(condition, columns);
    }

    @Override
    @SafeVarargs
    public final LambdaQueryChainWrapper<T> select(SFunction<T, ?>... columns) {
        return doSelect(true, CollectionUtil.toList(columns));
    }

    @Override
    @SafeVarargs
    public final LambdaQueryChainWrapper<T> select(boolean condition, SFunction<T, ?>... columns) {
        return doSelect(condition, CollectionUtil.toList(columns));
    }

    /**
     * @since 1.0.0
     */
    protected LambdaQueryChainWrapper<T> doSelect(boolean condition, List<SFunction<T, ?>> columns) {
        wrapperChildren.select(condition, columns);
        return typedThis;
    }

    @Override
    public LambdaQueryChainWrapper<T> select(Class<T> entityClass, Predicate<ColDesc> predicate) {
        wrapperChildren.select(entityClass, predicate);
        return typedThis;
    }

    @Override
    public String getSqlSelect() {
        throw new BeetlSQLException(BeetlSQLException.ERROR, String.format("can not use this method for \"%s\"", "getSqlSelect"));
    }

    @Override
    public BaseDao<T> getBaseDao() {
        return baseDao;
    }

    @Override
    public Class<T> getEntityClass() {
        return super.wrapperChildren.getEntityClass();
    }
}
